package com.xblibo.xruth.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xblibo.xruth.entity.User;
import com.xblibo.xruth.security.SecurityUserHolder;

/**
 * <p>Title: HeaderMenuBindFilter.java </p>
 * <p>Description:  </p>
 * <p>Copyright: 易宝 2011*版权所有 </p>
 * <p>Company: yeepay </p>
 * @author xblibo
 * @version 1.0 ,  2011  下午04:45:28
 */
public class HeaderMenuBinderFilter implements Filter {

	public static final Log log = LogFactory.getLog(HeaderMenuBinderFilter.class);
	
	
	public void destroy() {
		
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		String  serverName = request.getContextPath();
		String uri = request.getRequestURI().replace(serverName+"/", "");
		if(uri.indexOf("?")>0){
			uri = uri.substring(0,uri.indexOf("?"));
		}
		log.info("serverName:"+serverName+"  uri:"+uri);
		if(uri.indexOf("/")>0){
			String module = uri.substring(0,uri.indexOf("/"));
			String function = uri.substring(uri.indexOf("/")+1);
			log.info("module:"+module+" function:"+function);
			User user = SecurityUserHolder.getCurrentUser();
			request.setAttribute("functions", user.getModuleMenuFunction().get(module));
			request.setAttribute("currentModule",module);
			if("add".equals(function) || "modify".equals(function) || "detail".equals(function) || "delete".equals(function)){
				request.setAttribute("currentFunction","list");
			}else{
				request.setAttribute("currentFunction",function);
			}
		}
		
		chain.doFilter(req, res);
	}

	public void init(FilterConfig config) throws ServletException {
		config.getInitParameter("system");
	}

}
